package com.atguigu.day09;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink19_SQL_OverWindow {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);



        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        Configuration configuration = tableEnv.getConfig().getConfiguration();
        configuration.setString("table.local-time-zone", "GMT");

        //TODO 在DDL建表语句中指定事件时间字段
        tableEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int," +
                //在1.13没有出现新的函数的时候使用，但是会有精度的确实，只能精确到秒
//                "t as to_timestamp(from_unixtime(ts/1000,'yyyy-MM-dd HH:mm:ss')), " +
                //1.13版本之后新出的函数
                "t as to_timestamp_ltz(ts,3), " +
                "watermark for t as t - interval '3' second" +
                ") with(" +
                "'connector' = 'filesystem'," +
                "'path' = 'input/sensor-sql.txt'," +
                "'format' = 'csv'" +
                ")");

     //Over开窗
        tableEnv.executeSql("select " +
                "id," +
                "ts," +
                "vc," +
                "sum(vc) over w," +
                "count(vc) over w " +
                "from sensor " +
                "window w as (partition by id order by t)").print();

    }
}
